package com.jstyle.beatecg.ble;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;


/**
 * @author wyl
 *         后台运行帮助服务
 */
@SuppressLint({"NewApi"})
public class BackgroundRunningHelp extends Service{
    private MediaPlayer mediaPlayer;
    @Override
    public void onCreate() {
        super.onCreate();
    //初始化时就创建一个MediaPlayer进行资源链接
    mediaPlayer = MediaPlayer.create(this,com.jstyle.beatecg.R.raw.no_notice);
    }
    public BackgroundRunningHelp() {
        super();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        super.onStartCommand(intent,flags,startId);
        Log.e("BackgroundRunningHelp","onStartCommand");
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(!mediaPlayer.isPlaying()){
                    mediaPlayer.start();  // 开始播放
                    mediaPlayer.setLooping(true); // 允许循环播放
                }
            }
        }).start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();//自摸
        Intent i = new Intent(this, BackgroundRunningHelp.class);
        startService(i);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return false;//注意别写null
    }

}
